package com.haoyun.mirage.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.haoyun.mirage.user.mapper.SysRoleMapper;
import com.haoyun.mirage.user.model.entity.SysRole;
import com.haoyun.mirage.user.model.entity.SysRoleMenu;
import com.haoyun.mirage.user.mapper.SysRoleMenuMapper;
import com.haoyun.mirage.user.service.ISysRoleMenuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xiaoleilu.hutool.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单关联表 服务实现类
 * </p>
 *
 * @author twg
 * @since 2018-06-20
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    @Override
    @Transactional
    public Boolean insertRoleMenus(Long roleId, Long[] menuIds) {
        Assert.notNull(roleId,"角色id不能为空");
        Assert.notEmpty(menuIds,"菜单ids不能为空");
        this.delete(new EntityWrapper<SysRoleMenu>().eq("role_id",roleId));
        List<SysRoleMenu> roleMenuList = Lists.newArrayList();
        SysRoleMenu roleMenu = null;
        for (Long menuId : menuIds) {
            roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuList.add(roleMenu);
        }
        return this.insertBatch(roleMenuList);
    }
}
